package com.zhiwang.planet.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.PaintDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.Checkable;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.content.ContextCompat;

import com.zhiwang.planet.R;

public class StyleTab extends LinearLayout implements Checkable {

    private boolean mChecked = false;
    private int[] CHECKED_STATE_SET = new int[]{android.R.attr.state_checked};
    private int checkedColor;
    private int uncheckedColor;
    private int indicatorMargin;
    private int indicatorWidth;
    private int indicatorHeight;
    private int indicatorCorner;
    private CheckTextView textView;
    private View indicator;
    private String title;

    private int[][] states = new int[][]{
            new int[]{android.R.attr.state_checked},
            new int[]{-android.R.attr.checked}
    };

    public StyleTab(Context context) {
        this(context, null);
    }

    public StyleTab(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StyleTab(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);


        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.StyleTab);

        checkedColor = ta.getColor(R.styleable.StyleTab_checked_color, ContextCompat.getColor(context, R.color.colorPrimary));
        uncheckedColor = ta.getColor(R.styleable.StyleTab_unchecked_color, ContextCompat.getColor(context, R.color.colorPrimaryLight));
        indicatorMargin = ta.getDimensionPixelSize(R.styleable.StyleTab_indicator_margin, 0);
        indicatorWidth = ta.getDimensionPixelSize(R.styleable.StyleTab_indicator_width, 0);
        indicatorHeight = ta.getDimensionPixelSize(R.styleable.StyleTab_indicator_height, 0);
        indicatorCorner = ta.getDimensionPixelSize(R.styleable.StyleTab_indicator_corner, 0);
        title = ta.getString(R.styleable.StyleTab_title);
        mChecked = ta.getBoolean(R.styleable.StyleTab_checked, false);
        ta.recycle();


        setOrientation(LinearLayout.VERTICAL);

        textView = new CheckTextView(context);

        int[] colors = new int[]{checkedColor, uncheckedColor};
        ColorStateList colorStateList = new ColorStateList(states, colors);

        textView.setTextColor(colorStateList);
        textView.setText(title);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,18);

        LayoutParams tvLp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        addView(textView, tvLp);

        /*indicator*/
        indicator = new View(context);

        PaintDrawable shapeDrawable = new PaintDrawable();
        shapeDrawable.setShape(new RectShape());
        shapeDrawable.getPaint().setColor(Color.TRANSPARENT);
        shapeDrawable.setIntrinsicWidth(indicatorWidth);
        shapeDrawable.setIntrinsicHeight(indicatorHeight);
        shapeDrawable.setCornerRadius(indicatorCorner);

        indicator.setBackground(shapeDrawable);
        LayoutParams layoutParams = new LinearLayout.LayoutParams(indicatorWidth, indicatorHeight);
        layoutParams.topMargin = indicatorMargin;
        layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
        addView(indicator, layoutParams);

        setChecked(mChecked);

    }


    public void toggle() {
        setChecked(!mChecked);
    }

    public boolean isChecked() {
        return mChecked;
    }

    public void setChecked(boolean checked) {
        if (mChecked != checked) {
            mChecked = checked;
            refreshDrawableState();
        }

        textView.setChecked(checked);

        PaintDrawable paintDrawable = ((PaintDrawable) indicator.getBackground());

        if (mChecked) {
            paintDrawable.getPaint().setColor(checkedColor);
            indicator.invalidate();
        } else {
            paintDrawable.getPaint().setColor(Color.TRANSPARENT);
            indicator.invalidate();
        }
    }

    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
        int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isChecked()) {
            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
        }
        return drawableState;
    }

    @Override
    public int hashCode() {
        return title.hashCode();
    }

    @Override
    public boolean equals(@Nullable Object obj) {
        if (obj instanceof StyleTab) {

            StyleTab tab = (StyleTab) obj;
            return tab.title.equals(title);
        }
        return false;

    }
}


class CheckTextView extends AppCompatTextView implements Checkable {

    private boolean mChecked = false;
    private int[] CHECKED_STATE_SET = new int[]{android.R.attr.state_checked};

    public CheckTextView(Context context) {
        this(context, null);
    }

    public CheckTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CheckTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void toggle() {
        setChecked(!mChecked);
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public void setChecked(boolean checked) {
        if (mChecked != checked) {
            mChecked = checked;
            refreshDrawableState();
        }
    }

    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
        int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isChecked()) {
            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
        }
        return drawableState;
    }
}
